//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.net.URL;
import java.security.AccessController;
import java.security.PermissionCollection;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

final class JceSecurityManager extends SecurityManager {
    private static final javax.vpncrypto.CryptoPermissions defaultPolicy = javax.vpncrypto.JceSecurity.getDefaultPolicy();
    private static final javax.vpncrypto.CryptoPermissions exemptPolicy = javax.vpncrypto.JceSecurity.getExemptPolicy();
    private static final javax.vpncrypto.CryptoAllPermission allPerm;
    private static final Vector<Class<?>> TrustedCallersCache = new Vector(2);
    private static final ConcurrentMap<URL, javax.vpncrypto.CryptoPermissions> exemptCache = new ConcurrentHashMap();
    private static final javax.vpncrypto.CryptoPermissions CACHE_NULL_MARK = new javax.vpncrypto.CryptoPermissions();
    static final JceSecurityManager INSTANCE;

    private JceSecurityManager() {
    }

    javax.vpncrypto.CryptoPermission getCryptoPermission(String var1) {
        var1 = var1.toUpperCase(Locale.ENGLISH);
        javax.vpncrypto.CryptoPermission var2 = this.getDefaultPermission(var1);
        if (var2 == javax.vpncrypto.CryptoAllPermission.INSTANCE) {
            return var2;
        } else {
            Class[] var3 = this.getClassContext();
            URL var4 = null;

            int var5;
            for(var5 = 0; var5 < var3.length; ++var5) {
                Class var6 = var3[var5];
                var4 = javax.vpncrypto.JceSecurity.getCodeBase(var6);
                if (var4 != null) {
                    break;
                }

                if (!var6.getName().startsWith("javax.crypto.")) {
                    return var2;
                }
            }

            if (var5 == var3.length) {
                return var2;
            } else {
                javax.vpncrypto.CryptoPermissions var14 = (javax.vpncrypto.CryptoPermissions)exemptCache.get(var4);
                if (var14 == null) {
                    synchronized(this.getClass()) {
                        var14 = (javax.vpncrypto.CryptoPermissions)exemptCache.get(var4);
                        if (var14 == null) {
                            var14 = getAppPermissions(var4);
                            exemptCache.putIfAbsent(var4, var14 == null ? CACHE_NULL_MARK : var14);
                        }
                    }
                }

                if (var14 != null && var14 != CACHE_NULL_MARK) {
                    if (var14.implies(allPerm)) {
                        return allPerm;
                    } else {
                        PermissionCollection var7 = var14.getPermissionCollection(var1);
                        if (var7 == null) {
                            return var2;
                        } else {
                            Enumeration var8 = var7.elements();

                            while(var8.hasMoreElements()) {
                                javax.vpncrypto.CryptoPermission var9 = (javax.vpncrypto.CryptoPermission)var8.nextElement();
                                if (var9.getExemptionMechanism() == null) {
                                    return var9;
                                }
                            }

                            PermissionCollection var15 = exemptPolicy.getPermissionCollection(var1);
                            if (var15 == null) {
                                return var2;
                            } else {
                                var8 = var15.elements();

                                while(var8.hasMoreElements()) {
                                    javax.vpncrypto.CryptoPermission var10 = (javax.vpncrypto.CryptoPermission)var8.nextElement();

                                    try {
                                        ExemptionMechanism.getInstance(var10.getExemptionMechanism());
                                        if (var10.getAlgorithm().equals("*")) {
                                            javax.vpncrypto.CryptoPermission var11;
                                            if (var10.getCheckParam()) {
                                                var11 = new javax.vpncrypto.CryptoPermission(var1, var10.getMaxKeySize(), var10.getAlgorithmParameterSpec(), var10.getExemptionMechanism());
                                            } else {
                                                var11 = new javax.vpncrypto.CryptoPermission(var1, var10.getMaxKeySize(), var10.getExemptionMechanism());
                                            }

                                            if (var14.implies(var11)) {
                                                return var11;
                                            }
                                        }

                                        if (var14.implies(var10)) {
                                            return var10;
                                        }
                                    } catch (Exception var12) {
                                    }
                                }

                                return var2;
                            }
                        }
                    }
                } else {
                    return var2;
                }
            }
        }
    }

    private static javax.vpncrypto.CryptoPermissions getAppPermissions(URL var0) {
        try {
            return javax.vpncrypto.JceSecurity.verifyExemptJar(var0);
        } catch (Exception var2) {
            return null;
        }
    }

    private javax.vpncrypto.CryptoPermission getDefaultPermission(String var1) {
        Enumeration var2 = defaultPolicy.getPermissionCollection(var1).elements();
        return (javax.vpncrypto.CryptoPermission)var2.nextElement();
    }

    boolean isCallerTrusted() {
        Class[] var1 = this.getClassContext();
        URL var2 = null;

        int var3;
        for(var3 = 0; var3 < var1.length; ++var3) {
            var2 = javax.vpncrypto.JceSecurity.getCodeBase(var1[var3]);
            if (var2 != null) {
                break;
            }
        }

        if (var3 == var1.length) {
            return true;
        } else if (TrustedCallersCache.contains(var1[var3])) {
            return true;
        } else {
            try {
                javax.vpncrypto.JceSecurity.verifyProviderJar(var2);
            } catch (Exception var5) {
                return false;
            }

            TrustedCallersCache.addElement(var1[var3]);
            return true;
        }
    }

    static {
        allPerm = javax.vpncrypto.CryptoAllPermission.INSTANCE;
        INSTANCE = (JceSecurityManager)AccessController.doPrivileged(new PrivilegedAction<JceSecurityManager>() {
            public JceSecurityManager run() {
                return new JceSecurityManager();
            }
        });
    }
}
